<?php
$article_info_boxes =
array(
    "article_type" => array(
        "name" => "article_type",
        "std" => "请选择导航文章类别（同类别的文章将归纳到与类别名称相同的页面导航博文列表中。如果是商品推广文章，请不要选择该选项）",
        "title" => "导航文章类别：",
        "type" => "select",
        "data" => array("", "服装搭配", "鞋包配饰", "健康养生", "情感生活", "貌都鉴赏")),
);
function article_info_boxes() {
    global $post, $article_info_boxes;
    foreach($article_info_boxes as $meta_box) {
        $meta_box_value = get_post_meta($post->ID, $meta_box['name'] , true);
       // if($meta_box_value == "")
       //     $meta_box_value = $meta_box['std'];
        // 自定义字段标题
        echo'<h4>'.$meta_box['title'].'</h4><p>'.$meta_box['std'].'</p>';
        $html = "";
        // 自定义字段选择框
        if($meta_box['type'] == "select") {
            $html.= '<select name="'.$meta_box['name'].'">';
            foreach($meta_box['data'] as $option_item) {
                if($option_item == $meta_box_value)
                    $html.='<option value="'.$option_item.'" selected="true" >'.$option_item.'</option>';
                else
                    $html.='<option value="'.$option_item.'" >'.$option_item.'</option>';
            }
            $html.= '</select>';
        }
        // 自定义字段文本框
        else if($meta_box['type'] == "input") {
        } 
        echo $html;
        //echo '<textarea cols="80" rows="3" style="width:95%;" name="'.$meta_box['name'].'">'.$meta_box_value.'</textarea><br />';
    }
    echo '<input type="hidden" name="newmetaboxes_noncename" id="newmetaboxes_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
$new_meta_boxes =
array(
    "good_gourl" => array(
        "name" => "good_gourl",
        "std" => "请输入宝贝的直达链接，如 http://s.click.taobao.com.....",
        "title" => "直接链接："),
    	"good_price" => array(
	        "name" => "good_price",
	        "std" => "请输入宝贝的价格，如 190.00、89.50 ",
	        "title" => "宝贝价格：")
);
function new_meta_boxes() {
    global $post, $new_meta_boxes;

    foreach($new_meta_boxes as $meta_box) {
        $meta_box_value = get_post_meta($post->ID, $meta_box['name'] , true);
       // if($meta_box_value == "")
       //     $meta_box_value = $meta_box['std'];
        // 自定义字段标题
        echo'<h4>'.$meta_box['title'].'</h4><p>'.$meta_box['std'].'</p>';
        // 自定义字段输入框
        echo '<textarea cols="80" rows="3" style="width:95%;" name="'.$meta_box['name'].'">'.$meta_box_value.'</textarea><br />';
    }
    echo '<input type="hidden" name="newmetaboxes_noncename" id="newmetaboxes_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
function create_meta_box() {
    global $theme_name;
    if ( function_exists('add_meta_box') ) {
        add_meta_box( 'article-info-boxes', '文章信息', 'article_info_boxes', 'post', 'normal', 'high' );
        add_meta_box( 'new-meta-boxes', '商品信息', 'new_meta_boxes', 'post', 'normal', 'high' );
    }
}

function save_postdata( $post_id ) {
    global $new_meta_boxes, $article_info_boxes;
    if ( !wp_verify_nonce( $_POST['newmetaboxes_noncename'], plugin_basename(__FILE__) ))
        return;
    if ( !current_user_can( 'edit_posts', $post_id ))
        return;
    foreach($new_meta_boxes as $meta_box) {
        $data = $_POST[$meta_box['name']];
        if(get_post_meta($post_id, $meta_box['name']) == "")
            add_post_meta($post_id, $meta_box['name'], $data, true);
        else if($data != get_post_meta($post_id, $meta_box['name'], true))
            update_post_meta($post_id, $meta_box['name'] , $data);
        else if($data == "")
            delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
    }

    foreach($article_info_boxes as $meta_box) {
        $data = $_POST[$meta_box['name']];
        if(get_post_meta($post_id, $meta_box['name']) == "")
            add_post_meta($post_id, $meta_box['name'], $data, true);
        else if($data != get_post_meta($post_id, $meta_box['name'], true))
            update_post_meta($post_id, $meta_box['name'] , $data);
        else if($data == "")
            delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
    }
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
?>